﻿using System.ComponentModel;
using Aeon.Emulator;
using Aeon.Emulator.Dos.VirtualFileSystem;

namespace Aeon.Configuration
{
    /// <summary>
    /// Describes the configuration of a mounted drive.
    /// </summary>
    public sealed class MountedDrive : INotifyPropertyChanged
    {
        private DriveLetter driveLetter;
        private DriveType driveType;
        private IDriveConfiguration driveConfiguration;

        /// <summary>
        /// Initializes a new instance of the MountedDrive class.
        /// </summary>
        public MountedDrive()
        {
        }

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Gets or sets the mounted drive letter.
        /// </summary>
        public DriveLetter DriveLetter
        {
            get
            {
                return this.driveLetter;
            }
            set
            {
                if(value != this.driveLetter)
                {
                    this.driveLetter = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("DriveLetter"));
                }
            }
        }
        /// <summary>
        /// Gets or sets the mounted drive type.
        /// </summary>
        public DriveType DriveType
        {
            get
            {
                return this.driveType;
            }
            set
            {
                if(value != this.driveType)
                {
                    this.driveType = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("DriveType"));
                }
            }
        }
        /// <summary>
        /// Gets or sets the mounted drive configuration.
        /// </summary>
        public IDriveConfiguration DriveConfiguration
        {
            get
            {
                return this.driveConfiguration;
            }
            set
            {
                if(value != this.driveConfiguration)
                {
                    this.driveConfiguration = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("DriveConfiguration"));
                }
            }
        }

        /// <summary>
        /// Returns a deep copy of the MountedDrive instance.
        /// </summary>
        /// <returns>Deep copy of the MountedDrive instance.</returns>
        public MountedDrive Clone()
        {
            var newDrive = new MountedDrive()
            {
                DriveLetter = this.DriveLetter,
                DriveType = this.DriveType
            };

            var config = this.DriveConfiguration;
            if(config != null)
                newDrive.DriveConfiguration = config.Clone();

            return newDrive;
        }

        /// <summary>
        /// Returns the letter and type of the drive as a string.
        /// </summary>
        /// <returns>Letter and type of the drive as a string.</returns>
        public override string ToString()
        {
            return string.Format("{0} {1}", this.DriveLetter, this.DriveType);
        }

        /// <summary>
        /// Raises the PropertyChanged event.
        /// </summary>
        /// <param name="e">Information about the changed property.</param>
        private void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            var handler = this.PropertyChanged;
            if(handler != null)
                handler(this, e);
        }
    }
}
